package com.gy.flink;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;

import java.util.Arrays;

public class Union {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        DataStreamSource<Long> text1 = env.addSource(new MyFlinkSource()).setParallelism(1);
        DataStreamSource<Long> text2 = env.addSource(new MyFlinkSource()).setParallelism(1);

        DataStream<Long> textStream = text1.union(text2);


        DataStream<Long> mapStream = textStream.map(new MapFunction<Long, Long>() {
            @Override
            public Long map(Long aLong) throws Exception {
                System.out.println("map处理数据为：" + aLong);
                return aLong;
            }
        });

        //每2秒处理一次数据
        DataStream<Long> sum = mapStream.timeWindowAll(Time.seconds(2))
                .sum(0);

        sum.print().setParallelism(1);


        env.execute(Union.class.getSimpleName());
    }

}
